package com.junchao.peng.utils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 用于封装AJAX调用以后的JSON返回值 其中
 * 正确返回值: {state:1, data:返回数据, errorCode:错误码, errMsg:错误信息}
 * 错误返回值: {state:0, data:null, errorCode:错误码, errMsg:错误信息}
 * @author 24694
 */
@ApiModel("JsonResult json工具类")
public class JsonResult implements Serializable {
    private static final long serialVersionUID = 1L;
    public static final int SUCCESS = 1;
    public static final int ERROR = 0;

    /**
     * 返回是否成功的状态, 1表示成功, 0表示失败
     */
    @ApiModelProperty("state 状态, 1表示成功, 0表示失败")
    private int state;
    /**
     * 成功时候,返回的JSON数据
     */
    @ApiModelProperty("data state状态为1时返回的数据")
    private Object data;
    /**
     * 是错误时候的错误消息
     */
    @ApiModelProperty("errMsg state状态为0时返回的信息")
    private String errMsg;
    /**
     * 是错误时候的错误码
     */
    @ApiModelProperty("errCode state状态为0时返回的错误码")
    private int errCode;

    public JsonResult() {
        state = SUCCESS;
    }

    public JsonResult(int state, Object data, String errMsg, int errCode) {
        this.state = state;
        this.data = data;
        this.errMsg = errMsg;
        this.errCode = errCode;
    }

    public JsonResult(String errMsg, int errCode) {
        state = ERROR;
        data = null;
        this.errCode = errCode;
        this.errMsg = errMsg;
    }

    public JsonResult(Object data) {
        state = SUCCESS;
        if (data != null) {
            this.data = data;
        }
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }

    public int getErrCode() {
        return errCode;
    }

    public void setErrCode(int errCode) {
        this.errCode = errCode;
    }

    @Override
    public String toString() {
        return "JsonResult [state : " + state + ", data : " + data + ", errCode : " + errCode + ", errMsg : " + errMsg + "]";
    }

}
